/*******************************************************************************
 * Copyright 2010 Ben Knowles
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.midtro.attolog;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;

/**
 * Formats a stacktrace for the logger.
 * 
 * Adapted from the 'SimpleLog.ExceptionFormat' class.
 * 
 * @author Ben Knowles
 */
final class LoggerException extends Format {
	
	private static final long serialVersionUID = -4475227630133775801L;
	private static final String LINE_SEP = System.getProperty("line.separator");
	
	/**
	 * {@inheritDoc}
	 */
	public StringBuffer format(Object obj, StringBuffer buf, FieldPosition pos){
		if(!(obj instanceof Throwable)){
			throw new IllegalArgumentException(getClass().getName()+" only formats Throwable.");
		}
		
		final Throwable throwable = (Throwable) obj;
		buf.append(throwable);
		
		final StringWriter sw = new StringWriter();
		throwable.printStackTrace(new PrintWriter(sw));
		buf.append(LINE_SEP).append(sw.toString());
		return buf;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public Object parseObject(String source, ParsePosition pos){
		throw new UnsupportedOperationException();
	}
}
